module GYLite
{		
	/**下拉菜单, 可以使用ISkinTheme的GetMenu快速构建
	 * */
	export class GYMenu extends GYSkinContainer
	{
		public static default_autoSize:boolean = true;
		public static default_autoClose:boolean;
		protected _listV:GYListV;
		protected _filterArr:Array<any>;
		protected _sourceArr:Array<any>;
		protected _autoSize:boolean;
		protected _autoClose:boolean;
		protected _filterFunc:Function;		
		/*** 
		 * @param skin 参见MenuSkin
		 * @param size 菜单高度，如果输入负数，则为菜单项目数目，此时菜单高度s.height和项高度boxH要自己设置一下 
		 * @param getItemFunc():IItemRender 列表项的item对象返回函数
		 * @param getThisObject getItemFunc的this
		 * */
		public constructor(skin:any=null,size:number=92,getItemFunc:Function=null,getThisObject:any=null, setItemFunc:Function=null, setThisObject:any=null)
		{
			super();
			var s = this;
			this.skin = skin;			
			s._listV=new GYListV(size,getItemFunc?getItemFunc:s.getMenuItem,getThisObject?getThisObject:s,setItemFunc?setItemFunc:s.setMenuItem,setThisObject?setThisObject:s);
			s._listV.viewChangeCallParent = true;
			s.addElement(s._listV);
			s.initComponent();
		}
		protected initComponent():void
		{var s = this;
			s._autoSize = GYMenu.default_autoSize;
			s._autoClose = GYMenu.default_autoClose;
		}
		/**设置数据源*/
		public set dataProvider(val:Array<any>)
		{var s = this;
			s._sourceArr = val;
			s._filterArr = s._sourceArr.concat();			
			s._listV.dataProvider=s._filterArr;
			s.autoSize = s._autoSize;
		}
		public get dataProvider():Array<any>
		{var s = this;
			return s._listV.dataProvider;
		}
		/**纵向滚动条*/
		public get vScroller():GYScrollBarV
		{var s = this;
			return s._listV.vScroller;
		}
		/**菜单列表*/
		public get listV():GYListV
		{var s = this;
			return s._listV;
		}
		public set autoSize(val:boolean)
		{var s = this;
			s._autoSize = val;
			if(s._autoSize)
			{
				if(s._filterArr && s._filterArr.length < s._listV.boxs)
					s.height = s._filterArr.length * (s._listV.boxH + s._listV.padding) + s._listV.paddingBottom + s._listV.paddingTop;
				else
					s.height = (s._listV.boxs - 1) * (s._listV.boxH + s._listV.padding) + s._listV.paddingBottom + s._listV.paddingTop;
			}
		}
		/**是否自适应高度*/
		public get autoSize():boolean
		{var s = this;
			return s._autoSize;
		}
		/**滚动条 0自动 1显示 2不显示*/
		public set scrollerPolicy(val:number)
		{var s = this;
			s._listV.scrollerPolicy = val;
		}
		public get scrollerPolicy():number
		{var s = this;
			return s._listV.scrollerPolicy;
		}
		/**设置过滤函数s.setFilterFunc,此函数有一个参数Array源数组，一个参数Array过滤数组、一个参数result输入框的内容，过滤数组放入符合的选项
		 * val(Array;Array;String)
		 * */
		public setFilterFunc(val:Function)
		{var s = this;
			s._filterFunc = val;
		}
		/**使用过滤函数s.filterFunc过滤无用的选项
		 * @param 过滤的字符串参数
		 * */
		public userFilter(result:string):Array<any>
		{var s = this;
			if(s._filterFunc!=null)
				s._filterFunc(s._sourceArr,s._filterArr,result);
			s.autoSize = s._autoSize;
			s._listV.dataProvider = s._filterArr;
			return s._filterArr;
		}
		/**重写此方法，提供菜单项*/
		protected getMenuItem():IItemRender
		{var s = this;
			var item:MenuItem;
			item = new MenuItem;
			return item;
		}
		/**重写此方法，设置菜单项数据*/
		protected setMenuItem(item:IItemRender,data:any):void
		{var s = this;
			item.setData(data);
		}
		
		/**获取主题皮肤，自定义皮肤请实现IMenuSkin接口*/
		protected getThemeSkin():IGYSkin
		{var s = this;
			return GYSprite.skinTheme.GetMenuSkin();
		}
		/**外部点击自动关闭*/
		public get autoClose():boolean
		{var s = this;
			return s._autoClose;
		}

		public set autoClose(value:boolean)
		{var s = this;
			if(s._autoClose)
				s.removeEventListener(egret.Event.ADDED_TO_STAGE, s.addToStage,s);
			s._autoClose = value;
			if(s._autoClose)
				s.addEventListener(egret.Event.ADDED_TO_STAGE, s.addToStage,s);
		}
		private addToStage(e:egret.Event):void
		{var s = this;
			GYSprite.addStageDown(this, s.outsideRelease, s, true);
		}
		protected outsideRelease(e:GYTouchEvent):void
		{var s = this;
			if(e.currentTarget == e.target)
			{
				if(s.parent)
					(<any>s.parent).removeElement(this);
				return;
			}
			e.result = 1;
		}

//		public set width(val:number)
//		{var s = this;			
//			if(s._width == val)
//				return;
//			s._width = val;
//			s._skin.width = s.width;
//			s.invalidDisplay();
//		}		
//		public set height(val:number)
//		{var s = this;	
//			if(s._height == val)
//				return;
//			s._height = val;
//			s._skin.height = s.height;
//			s.invalidDisplay();
//		}
	}
}